package com.mms.DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mms.entity.Dept;
import com.mysql.cj.x.protobuf.MysqlxConnection.Close;

public class IDeptDaoIimpl implements IDeptDao {

	@Override
	public Dept finddeptviaID(int id) {
		String sql="SELECT * FROM `dept` WHERE id=?;";
		Connection conn=null;
		PreparedStatement stmt=null;
		ResultSet re=null;
		Dept newdept =null;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/KAOSHI", "root", "root");
			stmt=conn.prepareStatement(sql);
			stmt.setInt(1, id);
			re=stmt.executeQuery();
			newdept=new Dept();
			if (re.next()) {
				newdept.setLocation(re.getString("name"));
				newdept.setName(re.getString("location"));
				newdept.setId(id);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//			1
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				re.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return newdept;
	}

}
